package com.lashou.chunshen.lashoutuangou.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.lashou.chunshen.lashoutuangou.R;

/**
 * Created by Administrator on 2016/8/30.
 */

public class ToolBarHelper {
    //上下文 创建view的时候需要用到
    private Context mContext;
    //base view
    private FrameLayout mContentView;
    //用户定义的view
    private View mUserView;
    //toolbar
    private Toolbar mToolBar;
    //试图构造器
    private LayoutInflater mInflater;
    /*
      *两个属性
      * 1 toolbar是否悬浮在窗口之上
      * 2 toolbar的高度获取
     */
    private static int[] ATTRS = {
            R.attr.windowActionBarOverlay,
            R.attr.actionBarSize
    };
    public ToolBarHelper(Context mContext, int layoutResID) {
        this.mContext = mContext;
        mInflater = LayoutInflater.from(mContext);
        //初始化整个内容
        initContentView();
        //初始化用户定义的布局
        initUserView(layoutResID);
        //初始化toolbar
        initToolBar();
    }
    private void initContentView() {
        mContentView = new FrameLayout(mContext);
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        mContentView.setLayoutParams(params);
    }
    private void initUserView(int layoutResID) {
        mUserView = mInflater.inflate(layoutResID, null);
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        TypedArray typedArray = mContext.getTheme().obtainStyledAttributes(ATTRS);

        boolean overly = typedArray.getBoolean(0, false);
//        boolean overly = true;
        int toolBarSize = (int) typedArray.getDimension(1, mContext.getResources().getDimension(R.dimen.abc_action_bar_default_hright_material));

        typedArray.recycle();

        params.topMargin = overly ? 0 : toolBarSize;
        mContentView.addView(mUserView, params);
    }
    private void initToolBar() {
        View toolBar = mInflater.inflate(R.layout.home_title_bar, mContentView);
        mToolBar = (Toolbar) toolBar.findViewById(R.id.mytoolbar);
    }
    public Toolbar getToolBar() {
        return mToolBar;
    }

    public FrameLayout getContentView() {
        return mContentView;
    }
}
